package com.cpems.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cpems.common.core.page.TableDataInfo;
import com.cpems.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cpems.common.utils.StringUtils;
import com.cpems.system.domain.SysProjectInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.cpems.system.domain.bo.SysEnergyProjectBo;
import com.cpems.system.domain.vo.SysEnergyProjectVo;
import com.cpems.system.domain.SysEnergyProject;
import com.cpems.system.mapper.SysEnergyProjectMapper;
import com.cpems.system.service.ISysEnergyProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 各项目每日能效Service业务层处理
 *
 * @author lyonfive
 * @date 2025-03-20
 */
@RequiredArgsConstructor
@Service
public class SysEnergyProjectServiceImpl implements ISysEnergyProjectService {

    private final SysEnergyProjectMapper baseMapper;

    @Override
    public TableDataInfo<SysEnergyProject> selectPageEnergyList(SysEnergyProject energy, PageQuery pageQuery) {
        Page<SysEnergyProject> page = baseMapper.selectPageEnergyList(pageQuery.build(), this.buildQueryWrapper(energy));
        return TableDataInfo.build(page);
    }

    private Wrapper<SysEnergyProject> buildQueryWrapper(SysEnergyProject energy) {
        Map<String, Object> params = energy.getParams();
        QueryWrapper<SysEnergyProject> wrapper = Wrappers.query();
        wrapper.eq(ObjectUtil.isNotNull(energy.getProjectId()), "e.project_id", energy.getProjectId())
        .like(StringUtils.isNotBlank(energy.getProjectName()), "p.project_name", energy.getProjectName())
        .between(params.get("beginTime") != null && params.get("endTime") != null,"e.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }

    /**
     * 查询各项目每日能效
     */
    @Override
    public SysEnergyProjectVo queryById(Long energyId){
        return baseMapper.selectVoById(energyId);
    }

    /**
     * 查询各项目每日能效列表
     */
    @Override
    public TableDataInfo<SysEnergyProjectVo> queryPageList(SysEnergyProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysEnergyProject> lqw = buildQueryWrapper(bo);
        Page<SysEnergyProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询各项目每日能效列表
     */
    @Override
    public List<SysEnergyProjectVo> queryList(SysEnergyProjectBo bo) {
        LambdaQueryWrapper<SysEnergyProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysEnergyProject> buildQueryWrapper(SysEnergyProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysEnergyProject> lqw = Wrappers.lambdaQuery();
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            SysEnergyProject::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增各项目每日能效
     */
    @Override
    public Boolean insertByBo(SysEnergyProjectBo bo) {
        SysEnergyProject add = BeanUtil.toBean(bo, SysEnergyProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEnergyId(add.getEnergyId());
        }
        return flag;
    }

    /**
     * 修改各项目每日能效
     */
    @Override
    public Boolean updateByBo(SysEnergyProjectBo bo) {
        SysEnergyProject update = BeanUtil.toBean(bo, SysEnergyProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysEnergyProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除各项目每日能效
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
